The Ultimate Heapsort

نویسنده

  • Jyrki Katajainen
چکیده

A variant of Heapsort|named Ultimate Heapsort|is presented that sorts n elements in-place in (n log 2 (n + 1)) worst-case time by performing at most n log 2 n + (n) key comparisons and n log 2 n + (n) element moves. The secret behind Ultimate Heapsort is that it occasionally transforms the heap it operates with to a two-layer heap which keeps small elements at the leaves. Basically, Ultimate Heapsort is like Bottom-Up Heapsort but, due to the two-layer heap property, an element taken from a leaf has to be moved towards the root only O(1) levels, on an average. Let a1::n] be an array of n elements each consisting of a key and some information associated with this key. This array is a (maximum) heap if, for all i 2 f2; : : :; ng, the key of element abi=2c] is larger than or equal to that of element ai]. That is, a heap is a pointer-free representation of a left complete binary tree, where the elements stored are partially ordered according to their keys. Element a1] with the largest key is stored at the root. Elements abi=2c], a2i] and a2i + 1] (if they exist) are respectively stored at the parent, the left child and the right child of the node at which element ai] is stored. Heapsort is a classical sorting method that is described in almost all algo-rithmic textbooks. Heapsort sorts the given elements in ascending order with respect to their keys as follows: Input: Array a1::n] of n elements. Output: The elements in a1::n] rearranged in sorted order, i.e., the key of ai] should be smaller than or equal to that of ai+1], for all i 2 f1; 2; : : :; n?1g. 1. Rearrange a1::n] into a heap. 2. for i n step-1 until 2 do Exchange a1] and ai]. Remake a1::i ? 1] into a heap. According to Williams 29], who invented Heapsort and the heap data structure , Heapsort is a marriage of Treesort developed by Floyd 11] and Tournament-sort described, for instance, by Iverson 16, Section 6.4] (see also 13]). Both Treesort and Tournament-sort run in (n log 2 (n+1)) worst-case time and carry out n log 2 n + (n) key comparisons and n log 2 n + (n) element moves. For example, Treesort uses 2n + (1) pointers and 2n additional locations for elements , whereas Heapsort requires …

برای دانلود رایگان متن کامل این مقاله و بیش از 32 میلیون مقاله دیگر ابتدا ثبت نام کنید

ثبت نام

اگر عضو سایت هستید لطفا وارد حساب کاربری خود شوید

منابع مشابه

Pushing the Limits in Sequential Sorting

With re nements to the WEAK-HEAPSORT algorithm we establish the general and practical relevant sequential sorting algorithm RELAXED-WEAK-HEAPSORT executing exactly ndlog ne 2 ne + 1 n log n 0:9n comparisons on any given input. The number of transpositions is bounded by n plus the number of comparisons. Experiments show that RELAXED-WEAK-HEAPSORT only requires O(n) extra bits. Even if this space...

متن کامل

On the Performance of WEAK-HEAPSORT

Dutton (1993) presents a further HEAPSORT variant called WEAK-HEAPSORT, which also contains a new data structure for priority queues. The sorting algorithm and the underlying data structure are analyzed showing that WEAK-HEAPSORT is the best HEAPSORT variant and that it has a lot of nice properties. It is shown that the worst case number of comparisons is ndlog ne 2 ne + n dlog ne n log n+ 0:1n...

متن کامل

A New Data Structure for Heapsort with Improved Number of Comparisons

In this paper we present a new data structure for implementing heapsort algorithm for pairs of which can be simultaneously stored and processed in a single register. Since time complexity of Carlsson type variants of heapsort has already achieved a leading coefficient of 1, concretely n lgn + n lg lg n, and lower bound theory asserts that no comparison based in-place sorting algorithm can sort ...

متن کامل

A Comparative Study of Some Ingenious Sorting Algorithms on Modern Architectures

Heapsort, Quicksort and Mergesort are three ingenious sorting algorithms, which deserve special attention. In the past these algorithms were studied in detail, but the study was carried out on old machines and involved cache simulations. Over the years computer architecture has gone through radical changes. Therefore, a study valid on old architectures may not be valid on new architectures. The...

متن کامل

On the Best Case of Heapsort

Although discovered some 30 years ago, the Heapsort algorithm is still not completely understood. Here we investigate the best case of Heapsort. Contrary to Ž . claims made by some authors that its time complexity is O n , i.e., linear in the Ž . number of items, we prove that it is actually O n log n and is, in fact, approximately half that of the worst case. Our proof contains a construction ...

متن کامل

ذخیره در منابع من


  با ذخیره ی این منبع در منابع من، دسترسی به آن را برای استفاده های بعدی آسان تر کنید

عنوان ژورنال:

دوره   شماره 

صفحات  -

تاریخ انتشار 1998